#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import print_function
import os
import time
# set the number of threads you want to use before importing mxnet
os.environ['MXNET_CPU_WORKER_NTHREADS'] = '4'
import mxnet as mx
import numpy as np
import matplotlib.pyplot as plt
import cv2

print ('Hello,World.')

#os.system('wget http://data.mxnet.io/data/test_images.tar.gz')
#os.system('tar -xf test_images.tar.gz')

## opencv
#N = 10
#tic = time.time()
#for i in range(N):
#    bgr_image = cv2.imread('test_images/ILSVRC2012_val_00000001.JPEG', flags=1)
#    rgb_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2RGB)
#print(N/(time.time()-tic), 'images decoded per second with opencv')
#plt.imshow(rgb_image); plt.show()
#
## mx.image
#tic = time.time()
#for i in range(N):
#    img = mx.image.imdecode(open('test_images/ILSVRC2012_val_00000001.JPEG','rb').read())
#mx.nd.waitall()
#print(N/(time.time()-tic), 'images decoded per second with mx.image')
#rgb_image = img.asnumpy()
#print (type(rgb_image))
#plt.imshow(img.asnumpy()); plt.show()
#
#cv2.imwrite('test.jpg',rgb_image)

# load image
img = mx.image.imdecode(open('test_images/ILSVRC2012_val_00000001.JPEG','rb').read())
# resize to w x h, w = 100, h = 70
tmp = mx.image.imresize(img, 100, 70)
plt.imshow(tmp.asnumpy()); plt.show()
cv2.imwrite('test2.jpg',tmp.asnumpy())
print (tmp.asnumpy().shape)
# resize shorter edge to size while preserving aspect ratio
tmp = mx.image.resize_short(img, 100)
plt.imshow(tmp.asnumpy()); plt.show()

# crop a random w x h region from image
tmp, coord = mx.image.random_crop(img, (150, 200))
print(coord)
plt.imshow(tmp.asnumpy())
plt.show()

print (img.shape)
print (img.shape[0])
print (type(img.shape[0]))
# Other utility functions include fixed_crop, center_crop, color_normalize, and random_size_crop.
chips,shape = mx.image.center_crop(img,(150,200))
a, b = (150,200)
print (a)
print (b)
plt.imshow(chips.asnumpy()); plt.show()